package com.tbit.uqbike.webmanager.controller.tag;

import com.tbit.uqbike.object.business.UserInfo;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.tag.TagService;
import com.tbit.uqbike.webmanager.controller.BaseController;
import com.tbit.uqbike.webmanager.dto.TagDTO;
import com.tbit.uqbike.webmanager.req.tag.*;
import com.tbit.uqbike.webmanager.resp.tag.TagBaseInfo;
import com.tbit.uqbike.webmanager.resp.tag.TagResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 车辆标签
 *
 *
 * @author <a href="mailto:chen.chaoxiong@tbit.com.cn">ChenChaoXiong</a>
 * @since 2024/7/8
 */
@RestController
@Slf4j
@RequestMapping("/tag")
public class TagController extends BaseController {

    @Autowired
    private TagService tagService;

        /**
        * 创建标签
        *
        * @param req
        * @return
        */
        @PostMapping("/create")
        public Result<String> createTag(@RequestBody @Valid CreateTagReq req) {
            UserInfo userInfo =fetchUserInfo();
            TagDTO tagDTO = req.toTagDTO(userInfo);
            Long tagId = tagService.createTag(tagDTO);
            return Result.success(tagId.toString());
        }

        /**
        * 编辑标签
        *
        * @param req
        * @return
        */
        @PostMapping("/update")
        public Result<Boolean> updateTag(@RequestBody @Valid UpdateTagReq req) {
            TagDTO tagDTO = req.toTagDTO(fetchUserInfo());
            return Result.success(tagService.updateTag(tagDTO));
        }

        /**
        * 删除标签
        * 批量和单个删除
        * @param req
        * @return
        */
        @PostMapping("/delete")
        public Result<BatchOperationMsgVO<String>> deleteTag(@RequestBody @Valid DeleteTagReq req) {
            UserInfo userInfo = fetchUserInfo();
            List<TagDTO> tags = req.tagDTOList(userInfo);
            return Result.success(tagService.deleteTagBatch(tags));
        }


    /**
        * 查询车辆标签列表-分页
        *
        * @param req
        * @return
        */
        @GetMapping("/listPage")
        public Result<PageResult<List<TagResp>>> listTagPage(@ModelAttribute @Valid QueryTagListReq req) {
            return Result.success(tagService.listPage(req));
        }


    /**
     * 查询车辆标签列表-下拉框
     *
     * @param req
     * @return
     */
    @GetMapping("/list")
    public Result<List<TagBaseInfo>> listTag(@ModelAttribute @Valid TagBaseInfoListReq req) {
            return Result.success(tagService.list(req.getBrandId(),req.getTagStatus()));
        }


        /**
        * 启用标签
        *
        * @param req
        * @return
        */
        @PostMapping("/enabled")
        public Result<BatchOperationMsgVO<String>> enabledTag(@RequestBody @Valid EnableTagReq req) {
            UserInfo userInfo = fetchUserInfo();
            return Result.success(tagService.enableBatch(req.tagDTOList(userInfo)));
        }

        /**
        * 禁用标签
        *
        * @param req
        * @return
        */
        @PostMapping("/disabled")
        public Result<BatchOperationMsgVO<String>> disabledTag(@RequestBody @Valid DisableTagReq req) {
            UserInfo userInfo = fetchUserInfo();
            return Result.success(tagService.disableBatch(req.tagDTOList(userInfo)));
        }


}
